package org.budo.support.web.realip;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.budo.support.lang.util.StringUtil;
import org.budo.support.mvcs.Mvcs;

import lombok.Getter;
import lombok.Setter;

/**
 * @author limingwei
 */
@Getter
@Setter
public class RealIpRequestWrapper extends HttpServletRequestWrapper {
    private String[] remoteAddrHeaderNames;

    public RealIpRequestWrapper(ServletRequest request, String[] remoteAddrHeaderNames) {
        super((HttpServletRequest) request);

        this.setRemoteAddrHeaderNames(remoteAddrHeaderNames);
    }

    public String getRemoteAddr() {
        String remoteAddr = Mvcs.getRemoteAddr(this, this.getRemoteAddrHeaderNames());
        return StringUtil.isEmpty(remoteAddr) ? super.getRemoteAddr() : remoteAddr;
    }

    public String toString() {
        return super.toString() + ", wrapped(" + this.getRequest() + ")";
    }
}